cmake_minimum_required(VERSION 3.9)

set(CMAKE_CXX_STANDARD 14)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libs/hawk/core)

if (APPLE)
    set(__APPLE__ ON)
elseif (WIN32)
    set(__WIN__ ON)
    set(_CRT_SECURE_NO_WARNINGS ON)
else ()
    set(__UNIX__ ON)
endif ()

configure_file(qconf.h.in ${CMAKE_CURRENT_SOURCE_DIR}/qconf.h @ONLY)

if (NOT WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
endif ()

option(LIBUV_USE_STATIC "Link static libuv" ON)
option(ENABLE_STATIC "create static lib" ON)

if (ENABLE_STATIC)
    set(LIBRARY_TYPE STATIC)
else ()
    set(LIBRARY_TYPE SHARED)
endif ()

if (NOT WIN32)
    find_package(OpenSSL REQUIRED)
    find_package(LibUV REQUIRED)
endif ()

set(qio-headers
        qconf.h
        qio.h
        evt_loop.h
        fd_chan.h
        write_buf.h
        os_socket.h
        socket_util.h
        tcp_conn.h
        tcp_server.h
        tcp_acceptor.h
        ssl_util.h
        timer.h
        time_wheel.h
        time_wheel_handler.h
        buffer.h
        chan_buffer.h
        )

set(qio-src
        evt_loop.cpp
        fd_chan.cpp
        write_buf.cpp
        os_socket.cpp
        socket_util.cpp
        tcp_conn.cpp
        tcp_server.cpp
        tcp_acceptor.cpp
        ssl_util.cpp
        timer.cpp
        time_wheel.cpp
        time_wheel_handler.cpp
        chan_buffer.cpp
        )

add_library(
        qio
        ${LIBRARY_TYPE}
        ${qio-headers}
        ${qio-src}
)

target_include_directories(
        qio
        PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/libs
        ${LIBUV_INCLUDE_DIRS}
        ${OPENSSL_INCLUDE_DIR}
)

target_link_libraries(
        qio
        PUBLIC
        ${LIBUV_LIBRARIES}
        ${OPENSSL_LIBRARIES}
)